package com.tistory.credit.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.tistory.admin.common.util.StringUtil;
import com.tistory.common.vo.CommonReq;
import com.tistory.credit.vo.CreditDetailInfo;
import com.tistory.credit.vo.CreditReq;
import com.tistory.credit.vo.CreditRes;

@Repository("CreditDaoImpl")
public class CreditDaoImpl implements CreditDao {

	@Autowired
	SqlMapClientTemplate sqlMapClientTemplate;

	public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
		this.sqlMapClientTemplate = sqlMapClientTemplate;
	}

	@Override
	public List<CreditRes> getCreditMainList(CommonReq req) throws SQLException {
		List<CreditRes> creditMainList = sqlMapClientTemplate.queryForList("CreditSql.getCreditMainList",  req);
		return creditMainList;
	}


	@Override
	public Integer getCreditMainListCount(CommonReq req) throws SQLException {
		return (Integer) sqlMapClientTemplate.queryForObject("CreditSql.getCreditMainListCount", req);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer insertCreditInfo(List<CreditReq> creditList)	throws SQLException {
		Integer kk = 0;
		for(CreditReq vo : creditList){
			if(!vo.getCardComCd().equals("21")){
				kk=  (Integer)sqlMapClientTemplate.queryForObject("CreditSql.selectCreditInfo", vo);
				if(kk>0){
					sqlMapClientTemplate.delete("CreditSql.deleteCreditInfo", vo);
				}

				sqlMapClientTemplate.update("CreditSql.insertCreditInfo", vo);
			}
		}

		return kk;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer insertCreditCardInfo(List<CreditReq> creditList)	throws SQLException {
		Integer kk = 0;
		String compareDate ="";
		for(CreditReq vo : creditList){

			sqlMapClientTemplate.update("CreditSql.updateCreditCardInfo", vo);

			//			kk=  (Integer)sqlMapClientTemplate.queryForObject("CreditSql.selectCreditCardInfo", vo);
			//			if(kk>0){
			//				sqlMapClientTemplate.delete("CreditSql.deleteCreditCardInfo", vo);
			//			}
			//
			//			sqlMapClientTemplate.update("CreditSql.insertCreditCardInfo", vo);
		}

		return kk;
	}

	@Override
	public List<CreditReq> getCardSumList(String userId) throws SQLException {
		List<CreditReq> cardSumList = sqlMapClientTemplate.queryForList("CreditSql.getCardSumList",  userId);
		return cardSumList;
	}


	@Override
	public List<CreditReq> getWithDrawInfoList(CommonReq req) throws SQLException {
		List<CreditReq> withDrawInfoList = sqlMapClientTemplate.queryForList("CreditSql.getWithDrawInfoList",  req);
		return withDrawInfoList;
	}

	@Override
	public List<CreditDetailInfo> getWithDrawDetailInfoList(CommonReq req) throws SQLException {
		List<CreditDetailInfo> withDrawDetailInfoList =  new ArrayList<CreditDetailInfo>();
		CreditDetailInfo vo = new CreditDetailInfo();
		List<CreditReq> imsiDrawDetailInfoList = sqlMapClientTemplate.queryForList("CreditSql.getWithDrawDetailInfoList",  req);

		for (int i = 0 ; i < imsiDrawDetailInfoList.size() ; i++){
			boolean nextFlag = false;
			if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("10")){
				vo.setTransDate(imsiDrawDetailInfoList.get(i).getTransDate());
				vo.setBcProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setBcProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
				vo.setTransWeekDay(imsiDrawDetailInfoList.get(i).getTransWeekDay());

			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("11")){
				vo.setKbProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setKbProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("12")){
				if(StringUtil.isEmpty(imsiDrawDetailInfoList.get(i).getProdAmt())){
					vo.setKeProdAmt("0");
					vo.setKeProdCount("0");
				}else{
					vo.setKeProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
					vo.setKeProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
				}
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("13")){
				vo.setSsProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setSsProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("15")){
				vo.setHdProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setHdProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("16")){
				vo.setLtProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setLtProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("17")){
				vo.setShProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setShProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("18")){
				if(StringUtil.isEmpty(imsiDrawDetailInfoList.get(i).getProdAmt())){
					vo.setJbProdAmt("0");
					vo.setJbProdCount("0");
				}else{
					vo.setJbProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
					vo.setJbProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
				}
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("19")){
				vo.setHnProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setHnProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}else if(imsiDrawDetailInfoList.get(i).getCardComCd().equals("20")){
				vo.setNhProdAmt(imsiDrawDetailInfoList.get(i).getProdAmt());
				vo.setNhProdCount(imsiDrawDetailInfoList.get(i).getProdCount());
			}

			if (i  < imsiDrawDetailInfoList.size() - 1) {
				if (!imsiDrawDetailInfoList.get(i+1).getTransDate().equals( imsiDrawDetailInfoList.get(i).getTransDate())) {    // 다음건이 다르면 
					nextFlag = true;
				}
			}
			if (i == imsiDrawDetailInfoList.size()-1) {
				nextFlag = true;
			}

			if(nextFlag){


				if(StringUtil.isEmpty(vo.getBcProdAmt())){
					vo.setBcProdAmt("0");
					vo.setBcProdCount("0");
				}		

				if(StringUtil.isEmpty(vo.getKbProdAmt())){
					vo.setKbProdAmt("0");
					vo.setKbProdCount("0");
				}		

				if(StringUtil.isEmpty(vo.getKeProdAmt())){
					vo.setKeProdAmt("0");
					vo.setKeProdCount("0");
				}
				if(StringUtil.isEmpty(vo.getSsProdAmt())){
					vo.setSsProdAmt("0");
					vo.setSsProdCount("0");
				}		

				if(StringUtil.isEmpty(vo.getHdProdAmt())){
					vo.setHdProdAmt("0");
					vo.setHdProdCount("0");
				}		

				if(StringUtil.isEmpty(vo.getHdProdAmt())){
					vo.setSsProdAmt("0");
					vo.setSsProdCount("0");
				}		

				if(StringUtil.isEmpty(vo.getLtProdAmt())){
					vo.setLtProdAmt("0");
					vo.setLtProdCount("0");
				}


				if(StringUtil.isEmpty(vo.getShProdAmt())){
					vo.setShProdAmt("0");
					vo.setShProdCount("0");
				}

				if(StringUtil.isEmpty(vo.getJbProdAmt())){
					vo.setJbProdAmt("0");
					vo.setJbProdCount("0");
				}

				if(StringUtil.isEmpty(vo.getHnProdAmt())){
					vo.setHnProdAmt("0");
					vo.setHnProdCount("0");
				}					

				if(StringUtil.isEmpty(vo.getNhProdAmt())){
					vo.setNhProdAmt("0");
					vo.setNhProdCount("0");
				}	




				int totalHap = Integer.parseInt(vo.getBcProdAmt())
						+ Integer.parseInt(vo.getKbProdAmt())
						+ Integer.parseInt(vo.getKeProdAmt())
						+ Integer.parseInt(vo.getSsProdAmt())
						+ Integer.parseInt(vo.getHdProdAmt())
						+ Integer.parseInt(vo.getLtProdAmt())
						+ Integer.parseInt(vo.getShProdAmt())
						+ Integer.parseInt(vo.getJbProdAmt())
						+ Integer.parseInt(vo.getHnProdAmt())
						+ Integer.parseInt(vo.getNhProdAmt());
				int totalCount = Integer.parseInt(vo.getBcProdCount())
						+ Integer.parseInt(vo.getKbProdCount())
						+ Integer.parseInt(vo.getKeProdCount())
						+ Integer.parseInt(vo.getSsProdCount())
						+ Integer.parseInt(vo.getHdProdCount())
						+ Integer.parseInt(vo.getLtProdCount())
						+ Integer.parseInt(vo.getShProdCount())
						+ Integer.parseInt(vo.getJbProdCount())
						+ Integer.parseInt(vo.getHnProdCount())
						+ Integer.parseInt(vo.getNhProdCount());

				vo.setHapProdAmt(String.valueOf(totalHap));
				vo.setHapProdCount(String.valueOf(totalCount));

				withDrawDetailInfoList.add(vo); 
				vo = new CreditDetailInfo();
			}

		}


		return withDrawDetailInfoList;
	}

	@Override
	public List<CreditReq> getCardWithDrawDetailInfo(Map<String, Object> map) throws SQLException {
		List<CreditReq> cardWithDrawDetailInfo = sqlMapClientTemplate.queryForList("CreditSql.getCardWithDrawDetailInfo",  map);
		return cardWithDrawDetailInfo;
	}

	@Override
	public List<CreditReq> getCardBillDetailInfoList(Map<String,Object> map)throws SQLException {
		List<CreditReq> cardBillDetailInfoList =  new ArrayList<CreditReq>();

		List<CreditReq> getAvailableDateList = sqlMapClientTemplate.queryForList("CreditSql.getAvailableDateList",  map);
		for( CreditReq dataVo : getAvailableDateList){
			map.put("expectTransDate", dataVo.getExpectTransDate());
//			List<CreditReq> imsiCardBillDetailInfoList = sqlMapClientTemplate.queryForList("CreditSql.getCardBillDetailInfoListV1",  map);
			List<CreditReq> imsiCardBillDetailInfoList = sqlMapClientTemplate.queryForList("CreditSql.getCardBillDetailInfoListV2",  map);
			for( CreditReq resVo : imsiCardBillDetailInfoList){
				cardBillDetailInfoList.add(resVo);
			}
		}
		return cardBillDetailInfoList;
	}


	@Override
	public List<CreditReq> getCardBillSeperateDetailInfo(Map<String, Object> map) throws SQLException {
		List<CreditReq> cardBillSeperateDetailInfo = sqlMapClientTemplate.queryForList("CreditSql.getCardBillSeperateDetailInfo",  map);
		return cardBillSeperateDetailInfo;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer deleteCreditInfo(Map<String, Object> map) throws SQLException {
		Integer kk = 0;
		if(map.get("flag").equals("bank")){
			sqlMapClientTemplate.delete("CreditSql.deleteCreditBankDateInfo", map);	
		}if(map.get("flag").equals("realBank")){
			 kk =  (Integer) sqlMapClientTemplate.queryForObject("CreditSql.getCreditRealBankDateInfoCount", map);
			if(kk == 0){
				sqlMapClientTemplate.delete("CreditSql.deleteCreditRealBankDateInfo", map);
			}
		}else{
			sqlMapClientTemplate.delete("CreditSql.deleteCreditCardDateInfo", map);	
		}
		return kk;

	}


	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer insertCreditRealCardInfo(List<CreditReq> creditList)	throws SQLException {
		Integer kk = 0;
		for(CreditReq vo : creditList){
			if(!vo.getCardComCd().equals("21")){
				if(kk ==0){
					sqlMapClientTemplate.delete("CreditSql.deleteRealCreditCardInfo", vo);
				}
				sqlMapClientTemplate.update("CreditSql.insertRealCreditCardInfo", vo);
				kk++;
			}
		}
		return kk;
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Integer insertCreditRealBankInfo(List<CreditReq> creditList)	throws SQLException {
		Integer kk = 0;
		for(CreditReq vo : creditList){
			if(!vo.getCardComCd().equals("21")){
				kk=  (Integer)sqlMapClientTemplate.queryForObject("CreditSql.getRealBankCreditCardInfo", vo);
				if(kk ==0){
					sqlMapClientTemplate.update("CreditSql.insertRealBankCreditCardInfo", vo);
				}
			}

		}
		kk=0;
		for(CreditReq vo : creditList){
			if(kk ==0){
				sqlMapClientTemplate.delete("CreditSql.deleteRealBillBcInfo", vo);
				sqlMapClientTemplate.update("CreditSql.insertRealBillBcInfo", vo);
				kk++;
			}
		}
		return kk;
	}
	
	@Override
	public List<CreditReq> getMinusListDetailInfo(Map<String, Object> map) throws SQLException {
		List<CreditReq> minusListDetailInfo = sqlMapClientTemplate.queryForList("CreditSql.getMinusListDetailInfo",  map);
		return minusListDetailInfo;
	}	
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void insertPay(Map<String, Object> map) throws SQLException {
			sqlMapClientTemplate.insert("CreditSql.insertPay", map);	
	}	
	
	@Override
	public CreditReq getMinusDetailInfo(Map<String, Object> map) throws SQLException {
		CreditReq minusDetailInfo = (CreditReq) sqlMapClientTemplate.queryForObject("CreditSql.getMinusDetailInfo",  map);
		return minusDetailInfo;
	}	
	
	@Override
	public List<CreditRes> getRealBankList(CommonReq req) throws SQLException {
		List<CreditRes> realBankList = sqlMapClientTemplate.queryForList("CreditSql.getRealBankList",  req);
		return realBankList;
	}
	
	@Override
	public List<CreditReq> getCardFeeDetailInfo(Map<String, Object> map) throws SQLException {
		List<CreditReq> cardFeeDetailInfo = sqlMapClientTemplate.queryForList("CreditSql.getCardFeeDetailInfo",  map);
		return cardFeeDetailInfo;
	}		
	
	@Override
	public List<CreditReq> getMinusPayList(CommonReq req) throws SQLException {
		List<CreditReq> realBankList = sqlMapClientTemplate.queryForList("CreditSql.getMinusPayList",  req);
		return realBankList;
	}
	
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateChangeData(Map<String, Object> map) throws SQLException {
			sqlMapClientTemplate.insert("CreditSql.updateChangeData", map);	
	}	
	
	@Override
	public List<CreditReq> getPayProecssList(CommonReq req) throws SQLException {
		List<CreditReq> payProecssList = sqlMapClientTemplate.queryForList("CreditSql.getPayProecssList",  req);
		return payProecssList;
	}
}
